package cn.quang.search.toolkit.component.groups.impl;

import cn.quang.search.toolkit.component.items.impl.aggregation.ESAggregationColumn;
import cn.quang.search.toolkit.component.items.impl.aggregation.ESAggregationCondition;
import cn.quang.search.toolkit.component.items.impl.aggregation.ESAggregationConditionEnum;
import cn.quang.search.toolkit.component.items.impl.aggregation.ESAggregationFunctionEnum;
import cn.quang.search.toolkit.enums.DataSourceTypeEnum;
import cn.quang.search.toolkit.enums.SearchLogicType;
import cn.quang.search.toolkit.enums.SearchOperator;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ESAggregationSearchGroup {

    private final DataSourceTypeEnum dsType;
    private final Set<ESAggregationColumn> aggColumns;
    private final ESAggregationCondition aggCondition;
    private final SearchGroup searchGroup;

    public ESAggregationSearchGroup(DataSourceTypeEnum dsType){
        this.aggColumns = new HashSet<>();
        this.dsType = dsType;
        this.aggCondition = new ESAggregationCondition();
        this.searchGroup = new SearchGroup(dsType, SearchLogicType.AND);
    }

    public void addAggColumn(String dicName, String alias, ESAggregationFunctionEnum functionEnum){
        this.aggColumns.add(new ESAggregationColumn(dicName, alias, functionEnum));
    }

    public void addSearch(String col, SearchOperator operator, Object value){
        this.searchGroup.addSearch(col, operator, value);
    }

    public void addAggCondition(String col, ESAggregationConditionEnum aggConditionEnum){
        this.aggCondition.addAggregationCondition(col, aggConditionEnum);
    }

    public String buildAggSql(){
        String selectPart = resolveSelectField(this.aggColumns, this.aggCondition);
//        String conditionPart = searchGroup.buildWhereConditionSql();
        String aggConditionPart = aggCondition.getAggregationConditionSql();
//        return String.format("%s %s %s", selectPart + " from " + dsType.getTableName(), conditionPart, aggConditionPart);
        return StringUtils.EMPTY;
    }

    /**
     * 解析聚合查询的select部分
     * @param columns 统计列
     * @param aggCondition 聚合列
     */
    private String resolveSelectField(Set<ESAggregationColumn> columns, ESAggregationCondition aggCondition){
        List<String> sqlSlice = new ArrayList<>();
        for (ESAggregationColumn col : columns) {
            sqlSlice.add(col.getAggregationSql());
        }
        String aggregationColumnSql = aggCondition.getAggregationColumnSql();
        if(StringUtils.isNotBlank(aggregationColumnSql)){
            sqlSlice.add(aggregationColumnSql);
        }
        return "select " + StringUtils.join(sqlSlice, ", ");
    }

    public static void main(String[] args) {
        ESAggregationSearchGroup searchGroup = new ESAggregationSearchGroup(DataSourceTypeEnum.ES);
        searchGroup.addAggColumn("*", "ct", ESAggregationFunctionEnum.COUNT);

        searchGroup.addSearch("ownerBspId", SearchOperator.In, new String[]{"123","456","789"});

        searchGroup.addAggCondition("productLineId", ESAggregationConditionEnum.GROUP_BY);
        searchGroup.addAggCondition("cityId", ESAggregationConditionEnum.GROUP_BY);

        String aggSql = searchGroup.buildAggSql();
        System.out.println("sql=" + aggSql);
    }
}
